package com.quzitech.efest.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.quzitech.efest.activitys.base.BaseApplication;
import com.quzitech.efest.protocol.ConCreteCmd;
import com.quzitech.efest.protocol.MyRequest;
import com.tuner168.ble.BleService;

public class CmdLoopThread implements Runnable, IThread {
	BleService ble = null;
	String mac = "";
	Handler handler = null;
	Context context;
	private final static int MAINVIEW = 1;
	private final static int CHANGEBATTERYTYPE = 2;
	private final static int SEND_NO_TIME = 3;
	private final static int NOTYPE = 4;

	public CmdLoopThread(BleService ble, String mac, Context context) {
		this.ble = ble;
		this.mac = mac;
		this.context = context;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	@Override
	public void run() {
		Log.v("request", "i am in the run");
		Looper.prepare();
		handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				super.dispatchMessage(msg);
				boolean flag = ble.send(mac, (String) msg.obj, true);

				try {

					Thread.sleep(800);
				} catch (InterruptedException e) {

					e.printStackTrace();
				}
			}
		};
		Looper.loop();

	}

	@Override
	public void send_time(int SlotNo) {
		if (mac != null && !mac.equals("")) {
			if (handler == null) {

				return;
			}

			handler.obtainMessage(/* CHANGEBATTERYTYPE */NOTYPE,
					HexUtil.encode(MyRequest.getInstance().TimeLineCmd(SlotNo)))
					.sendToTarget();

			Log.v("request",
					"this is getvtlist---------"
							+ HexUtil.encode(MyRequest.getInstance()
									.QueryScreen()));

		}

	}

	// public void MySleep() {
	// try {
	//
	// Thread.sleep(2000);
	//
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }

	@Override
	public void sendmainview() {

		if (mac != null && !mac.equals("")) {
			if (handler == null) {

				return;
			}

			for (int i = 0; i < 5; i++) {
				Log.v("request",
						"i am in the send  start"
								+ "--------------------------------------------------------------");
				switch (i) {
				case 0:
					handler.obtainMessage(
							/* MAINVIEW */NOTYPE,
							HexUtil.encode(MyRequest.getInstance()
									.QueryScreen())).sendToTarget();

					BaseApplication.getLogger().debug(
							"\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--mian-->screen"
									+ "\nrequest:"
									+ HexUtil.encode(MyRequest.getInstance()
											.QueryScreen()));
					Log.v("request",
							"this is QueryScreen()---------"
									+ HexUtil.encode(MyRequest.getInstance()
											.QueryScreen()));
					break;
				case ConCreteCmd.IPASS.PASSNO1:
					handler.obtainMessage(
							/* MAINVIEW */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().PassCmd(
									ConCreteCmd.IPASS.PASSNO1))).sendToTarget();
					BaseApplication
							.getLogger()
							.debug("\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--mian-->pass1"
									+ "\nrequest:"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO1)));
					Log.v("request",
							"this is pass--1cmd---------"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO1)));

					break;
				case ConCreteCmd.IPASS.PASSNO2:
					handler.obtainMessage(
							/* MAINVIEW */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().PassCmd(
									ConCreteCmd.IPASS.PASSNO2))).sendToTarget();
					BaseApplication
							.getLogger()
							.debug("\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--mian-->pass2"
									+ "\nrequest:"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO2)));
					Log.v("request",
							"this is pass--2cmd---------"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO2)));

					break;
				case ConCreteCmd.IPASS.PASSNO3:
					handler.obtainMessage(
							/* MAINVIEW */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().PassCmd(
									ConCreteCmd.IPASS.PASSNO3))).sendToTarget();
					BaseApplication
							.getLogger()
							.debug("\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread---->pass3"
									+ "\nrequest:"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO3)));
					Log.v("request",
							"this is pass--3cmd---------"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO3)));

					break;
				case ConCreteCmd.IPASS.PASSNO4:
					handler.obtainMessage(
							/* MAINVIEW */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().PassCmd(
									ConCreteCmd.IPASS.PASSNO4))).sendToTarget();
					BaseApplication
							.getLogger()
							.debug("\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--mian-->pass4"
									+ "\nrequest:"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO4)));
					Log.v("request",
							"this is pass--4cmd---------"
									+ HexUtil
											.encode(MyRequest
													.getInstance()
													.PassCmd(
															ConCreteCmd.IPASS.PASSNO3)));

					break;

				}

			}

		}
	}

	@Override
	public void sentSlotandVtTime(int slotno) {
		if (mac != null && !mac.equals("")) {
			if (handler == null) {

				return;
			}
			for (int i = 0; i < 2; i++) {
				Log.v("request",
						"i am in the send slot start"
								+ "--------------------------------------------------------------");
				switch (i) {
				case 0:
					Log.v("request",
							"this is pass-----------"+slotno);
					handler.obtainMessage(
							/* SEND_NO_TIME */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().PassCmd(
									slotno))).sendToTarget();
					BaseApplication.getLogger().debug(
							"\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--slot-->pass-->"
									+ slotno
									+ "\nrequest:"
									+ HexUtil.encode(MyRequest.getInstance()
											.PassCmd(slotno)));
					break;

				case 1:
					Log.v("request",
							"this is pass---vt--------"+slotno);
					handler.obtainMessage(
							/* SEND_NO_TIME */NOTYPE,
							HexUtil.encode(MyRequest.getInstance().TimeLineCmd(
									slotno))).sendToTarget();
					BaseApplication.getLogger().debug(
							"\ntime:"
									+ MyDateUtils.getCurrentTime()
									+ "\ntype:thread--slot-->vtlist-pass->"
									+ slotno
									+ "\nrequest:"
									+ HexUtil.encode(MyRequest.getInstance()
											.TimeLineCmd(slotno)));
					break;

				}

			}
		}

	}

	@Override
	public void sendslot(int slotno) {
		if (mac != null && !mac.equals("")) {
			if (handler == null) {

				return;
			}
			// for (int i = 0; i < 1; i++) {
			Log.v("request",
					"i am in the send slot start"
							+ "--------------------------------------------------------------");
			// switch (i) {
			//
			//
			// case 0:
			handler.obtainMessage(NOTYPE,
					HexUtil.encode(MyRequest.getInstance().PassCmd(slotno)))
					.sendToTarget();

			// break;
			//
			// }

			// }
		}
	}

	@Override
	public void mSleep(int what) {

		handler.removeMessages(NOTYPE);

		// try {
		// Log.v("sleep", Thread.currentThread().getName());
		// Thread.sleep(secord * 1000);
		// } catch (InterruptedException e) {
		//
		// e.printStackTrace();
		// }

	}

}
